﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NicoView.ViewModel;
using NicoView.Comments;
using System.Threading;
using NicoView.Model;
using NicoView.Comments.Events;
using GalaSoft.MvvmLight.Messaging;
using NicoView.ViewModel.Events;
using NicoView.Model.Comments;

namespace NicoView
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public  MainWindow()
        {
            InitializeComponent();

            _comment = new NotifyNicoComment() { User = new NotifyNicoEncounterUser(new SerializableNicoEncounterUser("44458522")) { CustomName = "testName" }, No = 23, Elapsed = new TimeSpan(12, 30, 15), Body = "超GALS！寿蘭 ＯＰ「ア☆イ☆ツ」 3分25秒 sm4586291" };





            Messenger.Default.Register<NicoCommentMultiNoAnchorEventArgs>(this, OnMultiNoAnchor);

            Messenger.Default.Register<NicoCommentUrlShowEventArgs>(this, OnUrlShow);

            Messenger.Default.Register<NicoCommentVideoShowEventArgs>(this, OnVideoInfoShow);

            Messenger.Default.Register<NicoCommentLiveShowEventArgs>(this,OnLiveInfoShow);

            Messenger.Default.Register<NicoCommentCommunityShowEventArgs>(this, OnCommnityShow);

            Messenger.Default.Register<NicoCommentReplyRequestEventArgs>(this, OnReplyRequest);
        }

        private void OnReplyRequest(NicoCommentReplyRequestEventArgs obj)
        {
            MessageBox.Show(obj.Comment.DisplayNo);
        }

        private void OnCommnityShow(NicoCommentCommunityShowEventArgs obj)
        {
            MessageBox.Show(obj.CommunityId);
        }

        private void OnLiveInfoShow(NicoCommentLiveShowEventArgs obj)
        {
            MessageBox.Show(obj.LiveId);
        }

        private void OnVideoInfoShow(NicoCommentVideoShowEventArgs obj)
        {
            MessageBox.Show(obj.VideoId);
        }

        private void OnUrlShow(NicoCommentUrlShowEventArgs obj)
        {
            System.Diagnostics.Process.Start(obj.Url);
        }

        private void OnMultiNoAnchor(NicoCommentMultiNoAnchorEventArgs obj)
        {
            MessageBox.Show(obj.StartNo + "," +  obj.EndNo);
        }

        private void OnShowSingleUserInfo(NicoShowSingleUserInfoEventArgs obj)
        {
           
            MessageBox.Show(obj.User.Id);
        }

        Visibility visibility = System.Windows.Visibility.Visible;

        

        private NotifyNicoComment _comment;
        public NicoCommentListViewModel TargetViewModel
        {
            get
            {
                return (NicoCommentListViewModel)this.TargetControl.DataContext;
            }
        }

        public void SetComments()
        {
            Task.Factory.StartNew((sender) =>
            {
                ((SynchronizationContext)sender).Send((t) =>
                {
                    for (int index = 0; index < 200; index++)
                    {
                        TargetViewModel.Add(_comment);
                    }


                }, this);

            }, SynchronizationContext.Current);
        }

        private void TestButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            _comment.User.CustomName = "テスト名";

        }

        private void VisibuleTest_Click(object sender, System.Windows.RoutedEventArgs e)
        {

            visibility = visibility == System.Windows.Visibility.Hidden ? System.Windows.Visibility.Visible : System.Windows.Visibility.Hidden;


        }

        private  void AddTest_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SetComments();
            TargetViewModel.Comments.Upgrade();
        }

        private void ClerTest_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            TargetViewModel.Comments.Clear();
            GC.Collect();
        }


    }
}
